# coding = utf-8
import json
import xlrd
import os

# 遍历指定目录中的xls文件
def File_find(path):
    film = os.listdir(path)
    file_xls_list = []
    for each in film :
        if ".xls" in each :
            file_xls_list.append(each)
    # print(file_xls_list)
    return file_xls_list

# 针对分支和风机sheet页将列表型数据转化为字典型数据
def  List_convert_dict(key,values):
    sheet_list=[]
    corresp_num = 0
    row_num = 0
    while corresp_num < len(values):
        corresp_num += 1
        i_num = 0
        row_dict={}
        for i in key :
            row_dict.update({i:values[row_num][i_num]})
            i_num += 1
        sheet_list.append(row_dict)
        row_num += 1
    return sheet_list

# 读取xls文件
def Read_xls(path):
    all_excel_list = []
    for each in File_find(path):
        excel = xlrd.open_workbook(each)
        sheet_name_list = excel.sheet_names()
        # print(sheet_name_list)
        sheet_num = 0
        sheet_list = []
        while sheet_num < len(sheet_name_list) :
            sheet_data = excel.sheets()[sheet_num]
            sheet_list.append(sheet_data)
            sheet_num += 1
        # print( sheet_list)
        excel_dict ={}
        sheet_num = 0
        for i in sheet_list :
            nrows = i.nrows
            # print(nrows)
            sheet_name = sheet_name_list[sheet_num]
            sheet_num += 1
            if sheet_name == "配置参数" :
                row_num = 0
                keys_values_dict= {}
                while row_num < nrows :
                    keys = str(i.row_values(row_num, start_colx=0, end_colx=None)[0])
                    values = str(i.row_values(row_num, start_colx=0, end_colx=None)[1])
                    keys_values_dict.update({keys:values})
                    row_num += 1
                excel_dict.update({sheet_name:keys_values_dict})
                # print(nrows)
                #print(excel_dict)
                # print(i.row_values(row_num, start_colx=0, end_colx=None)
            elif sheet_name == "分支" or sheet_name == "风机" :
                row_num = 0
                values_list = [] 
                if row_num == 0 :
                    key_list = i.row_values(row_num, start_colx=0, end_colx=None)
                    # print(i.row_values(row_num, start_colx=0, end_colx=None))
                    row_num += 1
                    while row_num < nrows :
                        values_list.append(i.row_values(row_num, start_colx=0, end_colx=None))
                        row_num += 1
                # print(key_list)
                # print(values_list)
                    sheet_list = List_convert_dict(key_list,values_list)
                # print(sheet_dict)
                excel_dict.update({sheet_name:sheet_list})
        # print(excel_dict)
        all_excel_list.append(excel_dict)
    return all_excel_list

# 设置保存的json文件名字
def Save_json(path):
    excel_read_name = File_find(path)
    json_save_name = []
    for i in excel_read_name :
        j = i.split(".",2)
        json_save_name.append(j[0])
    return json_save_name

# 将数据转化为json文件
def Data_convert_json(path):
    data_list = Read_xls(path)
    excel_list = File_find(path)
    name_list = Save_json(path)
    film_path = os.chdir("D:/hello Python/邓老师任务/test1/result/json")
    excel_num = 0
    while excel_num < len(excel_list):
        data = data_list[excel_num]
        with open('%s.json'%name_list[excel_num], 'w',encoding='utf-8') as f:
            json.dump(data,f,ensure_ascii=False, indent=4)
            print("生成成功")
        excel_num += 1


def main():
    film_path = os.chdir("./data/xls")
    Data_convert_json(film_path)

if __name__ == '__main__':
    main()
    